//
//  HometwoViewController.m
//  SinceLe
//
//  Created by 李元喜 on 15/11/24.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import "HometwoViewController.h"
#import "HometwoCell.h"
#import "headerView.h"
#import "NetWorkHandle.h"
#import "MusicModel.h"
#import "Macrocs.h"
#import <UIImageView+WebCache.h>
@interface HometwoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) headerView *headerView;
@property (nonatomic, strong) UIView *oneview;
@property (nonatomic, strong) NSMutableArray *dataArraytwo;
@end

@implementation HometwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadTableView];
    [self loadHometwoData];
    self.view.backgroundColor = [UIColor whiteColor];

    

    
}

-(void)loadTableView{
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    _tableView.dataSource = self;
    _tableView.delegate = self;
     _headerView  = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil] firstObject];
    _headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
    _tableView.tableHeaderView = _headerView;
    
    UINib *nib = [UINib nibWithNibName:@"HometwoCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
  
    [self.view addSubview:_tableView];
}



//数据解析
-(void)loadHometwoData{
    //_id.传值到详细界面
    NSString *Str = [NSString stringWithFormat:KhomeUrl,_id];
    [NetWorkHandle getDataWithURLString:Str compare:^(id object) {
        _dataArraytwo = [NSMutableArray array];
        _dataArray = [NSMutableArray array];
        
        NSDictionary *dic = object;
        NSDictionary* dicc = [NSDictionary dictionary];
        dicc = dic[@"data"];
        
        //对头部view进行kvc
        MusicModel* headModel = [[MusicModel alloc] init];
        [headModel setValuesForKeysWithDictionary:dicc];
        _headerView.MusicModel = headModel;
        
        
        NSLog(@"_dataArray====%@",_dataArray);
        NSArray *arr = dic[@"data"][@"songs"];
        
        for (NSDictionary *a in arr) {
            MusicModel *model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:a];
            
            [_dataArraytwo addObject:model];
        }
        
        [_tableView reloadData];
    }];
    
}



//cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}

//返回多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArraytwo.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    HometwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   
//赋值
    MusicModel * model = _dataArraytwo[indexPath.row];
    [cell.twocellimage sd_setImageWithURL:[NSURL URLWithString:model.imageurl]];
    cell.twocellname.text = model.name;
    cell.twooneltitle.text = model.artist;
    
    
     return cell;

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
